// Echart
var eventJson = $(".scau-chart").data("event");
console.log("eventJson:-->>"+eventJson)
if(eventJson){
	var city = [eventJson[0].records[0].address.name];
	var index = 0;
	for (let arr of eventJson){
		for(let record of arr.records){
			let flag = 1;
			for(let c in city){
				if(city[c] == record.address.name){
					flag = 0;
					break;
				}
			}
			if(flag){
				city[index] = record.address.name;
				index++;
			}
		}
	}
	// console.log(city);

	var time = [eventJson[0].records[0].date.year];
	index = 0;
	for (let arr of eventJson){
		for(let record of arr.records){
			let flag = 1;
			for(let t in time){
				if(time[t] == record.date.year){
					flag = 0;
					break;
				}
			}
			if(flag){
				time[index] = record.date.year;
				index++;
			}
		}
	}
	time.sort();
	// console.log(time);

	var cityData = new Array();
	for(let c in city){
		cityData[c] = new Array();
		for(let t in time){
			cityData[c][t] = 0;
		}
	}
	for (let arr of eventJson){
		for(let record of arr.records){
			for(let c in city){
				if(record.address.name == city[c]){
					for(let t in time){
						if(record.date.year == time[t]){
							cityData[c][t]++;
						}
					}
				}
			}
		}
	}
}
/*
 * var city = [eventJson[0].records[0].address.name]; var index = 0; for (let
 * arr of eventJson){ for(let record of arr.records){ let flag = 1; for(let c in
 * city){ if(city[c] == record.address.name){ flag = 0; break; } } if(flag){
 * city[index] = record.address.name; index++; } } } // console.log(city);
 * 
 * var time = [eventJson[0].records[0].date.year]; index = 0; for (let arr of
 * eventJson){ for(let record of arr.records){ let flag = 1; for(let t in time){
 * if(time[t] == record.date.year){ flag = 0; break; } } if(flag){ time[index] =
 * record.date.year; index++; } } } time.sort(); // console.log(time);
 * 
 * var cityData = new Array(); for(let c in city){ cityData[c] = new Array();
 * for(let t in time){ cityData[c][t] = 0; } } for (let arr of eventJson){
 * for(let record of arr.records){ for(let c in city){ if(record.address.name ==
 * city[c]){ for(let t in time){ if(record.date.year == time[t]){
 * cityData[c][t]++; } } } } } }
 */
// console.log(cityData);
/*
 * var time = new Map([ [eventJson[0].records[0].date.year, 0] ]); for (let arr
 * of eventJson){ for(let record of arr.records){ let fgt = 1;
 * 
 * for(let t of time.entries()){ let city = new Map(); for (let arr of
 * eventJson){ for(let record of arr.records){ city.set(record.address.name, 0); } }
 * console.log(city); let city = t[1]; if( record.date.year == t[0]){ let fgc =
 * 1; for(let c of city.entries()){ if(record.address.name == c[0]){
 * city.set(c[0], c[1]+1); fgc = 0; break; } } if(fgc)
 * city.set(record.address.name, 1); time.set(t[0], city); fgt = 0; } //
 * console.log(city); } if(fgt){ let fgc = 1; let city = new Map(); for(let c of
 * city.entries()){ if(record.address.name == c[0]){ city.set(c[0], c[1]+1); fgc =
 * 0; break; } } if(fgc) city.set(record.address.name, 1);
 * time.set(record.date.year, city); // console.log(city); } } }
 */
// console.log(time);

var yaxis = [];
var chartType = 'line';
for (let c in city) {
	yaxis.push({
		name : city[c],
		type : chartType,
		stack : city[c],
		data : cityData[c]
	})
};

var option = {
	title : {
		text : '各地区事件发生趋势',
		top : '4%'
	},
	tooltip : {
		trigger : 'axis',
		axisPointer : {
			type : 'cross',
			label : {
				backgroundColor : '#6a7985'
			}
		}
	},
	legend : {
		data : city
	},
	toolbox : {
		feature : {
			saveAsImage : {}
		}
	},
	grid : {
		left : '3%',
		right : '4%',
		bottom : '3%',
		containLabel : true
	},
	xAxis : [ {
		type : 'category',
		boundaryGap : false,
		data : time
	} ],
	yAxis : [ {
		type : 'value',
	} ],
	series : yaxis
};

var myChart = echarts.init(document.getElementById('chartmain'));
myChart.setOption(option);

function changeXYAxis() {
	var that = event.currentTarget;
	var id = $(that).attr("id")
	yaxis.length = 0;
	if (id == 'scau-cityTime') {
		option.legend.data = city.concat()
		option.xAxis[0].data = time.concat()
		for (let c in city) {
			yaxis.push({
				name : city[c],
				type : chartType,
				stack : city[c],
				data : cityData[i]
			})
		}
	} else if (id == 'scau-kindCity') {
		option.legend.data = kind.concat()
		option.xAxis[0].data = [...city.keys()].concat()
		for (var i = 0; i < kind.length; i++) {
			yaxis.push({
				name : kind[i],
				type : chartType,
				stack : kind[i],
				data : kindData[i]
			})
		}
	}
	option.series = yaxis.concat()
	myChart.setOption(option, true);
	// true 表示不合并上次数据
}

function changetype() {
	var that = event.currentTarget;
	var id = $(that).attr("id");
	if (id == 'scau-linechart') {
		chartType = 'line'
		option.tooltip.axisPointer.type = 'line'
		option.xAxis[0].boundaryGap = false
	} else if (id == 'scau-barchart') {
		chartType = 'bar'
		option.tooltip.axisPointer.type = 'shadow'
		option.xAxis[0].boundaryGap = true
	}
	for (var i = 0; i < yaxis.length; i++) {
		yaxis[i].type = chartType
	}
	option.series = yaxis.concat()
	console.log(option.series[0].barwidth)
	myChart.setOption(option, true);
}
